package rma.flp;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.datastore.Blob;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Picture implements Serializable { // Cached objects need to be serializable

	private static final long serialVersionUID = 1L;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;

	@Persistent
	String ownerEmail;

	@Persistent
	String filename;
	
	@Persistent
	String description;
	
	@Persistent
	Date importedOn;
	
	/*
	 * GAE only accepts 500 bytes in a byte array,
	 * use Blob for larger objects.
	 */
	@Persistent(defaultFetchGroup="true")
	Blob imageData;

	public Picture(String ownerEmail, String filename, String description, byte[] bytes) {
		this.ownerEmail = ownerEmail;
		this.filename = filename;
		this.description = description;
		this.importedOn = new Date();
		this.imageData = new Blob(bytes);
	}

	public Long getId() {
		return id;
	}
	
	public String getOwnerEmail() {
		return ownerEmail;
	}
	
	public String getFilename() {
		return filename;
	}

	public String getDescription() {
		return description;
	}

	public Date getImportedOn() {
		return importedOn;
	}

	public byte[] getBytes() {
		return imageData.getBytes();
	}

	public String getType() {
		return StringUtils.substringAfterLast(filename, ".");
	}
	
}
